# encoding = utf-8
class Product < ActiveRecord::Base
  belongs_to :type
  has_many :carts
  has_many :orders
  has_many :comments
  has_many :deals, :through => :orders

  validates :name, :presence  => true
  validates :type_id, :presence  => true
  validates :price, :presence  => true
  validates :discount, :presence  => true, :numericality => true
  validates :stock, :presence  => true

  has_attached_file :avatar,
    :default_url => '/images/default_avatar_:style.jpg',
    :path => ":rails_root/public/system/:attachment/:id/:style/:normalized_product_avatar_name",
    :url => "/system/:attachment/:id/:style/:normalized_product_avatar_name",
    :styles => {:big => "190x190>", :small => '80x80' }
  
  def normalized_product_avatar_name
    "#{self.type.id}-#{self.id}-#{self.avatar_updated_at.strftime('%Y%m%d%H%m%S')}"
  end
end
